//
//  BindCardViewModel.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/3/9.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import Foundation
import HandyJSON

enum BindCardStatus: Int {
    case canBind
    case changePhone
    case noCard
}

struct BindCardModel: HandyJSON {
    var patientId: String?
    var name: String?
    var phone: String?
    var operationType: Int = -1
    var ownExpenseNum: String?
    var socialSecurityNum: String?
    var endPhoneNo: String?
    var hosPhoneNo: String?
}

struct BindCardViewModel {
    let model: BindCardModel
    
    var phone: String { return model.phone ?? "" }
    var status: BindCardStatus { return BindCardStatus(rawValue: model.operationType) ?? .canBind }
    var patientId: String { return model.patientId ?? "" }
    var endPhoneNo: String { return "xxxxxxx\(model.endPhoneNo ?? "")"}
    /// 医院预留的手机号
    var hosPhoneNo: String { model.hosPhoneNo ?? "" }
    /// 显示医院预留手机号 隐藏中间四位
    var hosPhoneNoFour: String {
        if !hosPhoneNo.isEmpty && hosPhoneNo.count >= 11 {
            let startIndex = hosPhoneNo.index(hosPhoneNo.startIndex, offsetBy: 3)
            let endIndex = hosPhoneNo.index(hosPhoneNo.startIndex, offsetBy: 7)
            return hosPhoneNo.replacingCharacters(in: startIndex...endIndex, with: "****")
        }
        return hosPhoneNo
    }
    
    var msgCode: String = ""
    
    /// 获取卡片数据
    static func getBindCardList(patientId: String, success: @escaping (_ model: BindCardModel) -> Void, failure: @escaping (_ errorMsg: String?) -> Void) {
        let paras = ["patientId": patientId]
        HttpTool.shared.request(target: ApiManager.getBindCardList(paras: paras), success: { (json, code) in
            YSGLog("获取医院绑卡信息: \(json)")
            switch code {
            case .failure:
                failure(json["message"].description)
            case .success:
                if let model = JSONDeserializer<BindCardModel>.deserializeFrom(json: json["data"].description) {
                    success(model)
                } else {
                    failure("请求数据发生错误，稍后重试")
                }
            }
        }) { errorMsg in
            failure(errorMsg)
        }
    }
    
    /// 获取短信验证码
    static func sendSmsCode(phone: String,
                     finished: @escaping (_ errorMsg: String?, _ code: HttpCode) -> Void)
    {
        let paras = ["phone": phone.encryptWithAES()]
        HttpTool.shared.request(target: ApiManager.sendSmsCode(paras: paras), success: { (json, code) in
            finished(json["message"].description, code)
        }) { errorMsg in
            finished(errorMsg, .failure)
        }
    }
    
    /// 新增卡片
    static func addCard(patientId: String, code: String, finished: @escaping (_ errorMsg: String?, _ code: HttpCode) -> Void) {
        let paras = ["patientId": patientId, "code": code]
        HttpTool.shared.request(target: ApiManager.addCard(paras: paras), success: { (json, code) in
            finished(json["message"].description, code)
        }) { errorMsg in
            finished(errorMsg, .failure)
        }
    }
    
    /// 绑定卡片
    static func bindCard(patientId: String, phone: String, code: String, finished: @escaping (_ errorMsg: String?, _ code: HttpCode) -> Void) {
        let paras = ["patientId": patientId, "hosPhoneNo": phone, "code": code]
        HttpTool.shared.request(target: ApiManager.bindCard(paras: paras), success: { (json, code) in
            finished(json["message"].description, code)
        }) { errorMsg in
            finished(errorMsg, .failure)
        }
    }
    
    /// 提交患者手机审核
    static func changePatientPhone(patientId: String, phone: String, resourceIds: String, finished: @escaping (_ errorMsg: String?, _ code: HttpCode) -> Void) {
        let paras = ["patientId": patientId, "phone": phone.encryptWithAES(), "resourceIds": resourceIds]
        HttpTool.shared.request(target: ApiManager.changePatientPhone(paras: paras), success: { (json, code) in
            finished(json["message"].description, code)
        }) { errorMsg in
            finished(errorMsg, .failure)
        }
    }
}
